﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirmaTransportowa
{
    public class StatisticsPanelPresenter
    {
        private User user;
        private StatisticsPanelForm statisticsPanelView;
        private StatisticsDriverStatisticsForm statisticsDriverStatisticsView;
        private StatisticsDriversStatisticsForm statisticsDriversStatisticsView;

        public StatisticsPanelPresenter(User user)
        {
            this.user = user;
            this.statisticsPanelView = new StatisticsPanelForm(this);
            this.showLoggedUserName();
            statisticsPanelView.Show();
        }

        public void clean()
        {
            if (statisticsPanelView != null) statisticsPanelView.Dispose();
            if (statisticsDriversStatisticsView != null) statisticsDriversStatisticsView.Dispose();
        }

        public void showDriverStatisticsView()
        {
            if (statisticsDriverStatisticsView == null || statisticsDriverStatisticsView.IsDisposed)
            {
                statisticsDriverStatisticsView = new StatisticsDriverStatisticsForm(this);
                displayDriverStatisticsListBox();
                statisticsDriverStatisticsView.Show();
            }
        }

        public void showDriversStatisticsView()
        {
            statisticsDriversStatisticsView = new StatisticsDriversStatisticsForm(this);
            displayDriversStatisticsDataGrid();
            statisticsDriversStatisticsView.Show();
        }

        private void displayDriverStatisticsListBox()
        {
            List<String> driversNamesAndSurnames = Worker.getDriversNamesAndSurnames();
            foreach (String nameSurname in driversNamesAndSurnames)
            {
                statisticsDriverStatisticsView.driversListBox.Items.Add(nameSurname);
            }
        }

        private void displayDriversStatisticsDataGrid()
        {
            statisticsDriversStatisticsView.statisticsGridView.DataSource = Transport.getDriversStatistics();
            statisticsDriversStatisticsView.showDataTable();
        }

        private void showLoggedUserName()
        {
            statisticsPanelView.statisticsName.Text = user.worker.name + " " + user.worker.surname;
        }
    }
}
